#include "quickoperationwidget.h"

quickOperationWidget::quickOperationWidget(QObject *parent)
{

    initTranslations();       // 初始化翻译文件

    // 初始化主界面
    m_pMainWidget = new MainWidget();
    m_pMainWidget->setContentsMargins(0,0,0,0);

    // 初始化TabWidget
    m_pMainTabWidget = new mainTabWidget();
    m_pMainTabWidget->setFixedSize(QUICKOPERATION_WIDTH, QUICKOPERATION_HEIGHT);
    m_pMainTabWidget->tabBar()->setFixedWidth(QUICKOPERATION_WIDTH);

    // 初始化快捷操作面板
    m_shortcutPanelWidget = new shortcutPanelWidget();
    m_pMainTabWidget->addTab(m_shortcutPanelWidget,tr(""));

    // 获取键盘事件
    connect(m_pMainTabWidget,&mainTabWidget::currentChanged,[=](int index){
        index ? m_shortcutPanelWidget->m_pScrollingAreaWidgetMajor->releaseKeyboard()
              : m_shortcutPanelWidget->m_pScrollingAreaWidgetMajor->grabKeyboard();
    });

    // 初始化剪贴板
    m_SidebarClipboardPlugin = new SidebarClipboardPlugin();
    m_pMainTabWidget->addTab(m_SidebarClipboardPlugin,"");

    // 初始化 & 布局 Tab抬头
    m_shortcutPanelLabel = new QLabel(tr("Shortcut Panel"));
    m_shortcutPanelLabel->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);

    m_clipboardLabel = new QLabel(tr("Clipboard"));
    m_clipboardLabel->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);

    m_quickOperLayout = new QHBoxLayout();
    m_quickOperLayout->setContentsMargins(0,0,0,0);

    m_quickOperLayout->addWidget(m_shortcutPanelLabel);
    m_quickOperLayout->addWidget(m_clipboardLabel);

    m_pMainTabWidget->tabBar()->setLayout(m_quickOperLayout);
    m_pMainTabWidget->tabBar()->setProperty("setRadius",12);

    // 去掉中间的分割线
    m_pMainTabWidget->tabBar()->setProperty("useTabbarSeparateLine", false);

    // 背景透明 解决切换黑屏问题
//    m_pMainTabWidget->setAttribute(Qt::WA_TranslucentBackground, true);

    QHBoxLayout *m_laby = new QHBoxLayout();
    m_laby->addWidget(m_pMainTabWidget);
    m_pMainWidget->setLayout(m_laby);


    updateTabWidget();
    connect(StyleGsetting::getInstance(),&StyleGsetting::Sig_styleGsettingChanged,[=](QString key){
        if (key == UKUI_STYLE_NAME_KEY) {
            updateTabWidget();
        }
    });
//     m_pMainTabWidget->setStyleSheet("QWidget{border:1px solid rgba(255,0,0,1);}");//测试用
}

QWidget *quickOperationWidget::getQuickOperationWidget()
{
    if (m_pMainWidget == nullptr) {
        return nullptr;
    }
    return m_pMainWidget;
}

void quickOperationWidget::initTranslations()
{
    QTranslator *translator = new QTranslator;
    if (translator->load(QLocale(), QLatin1String("ukui-quick-operation-panel"), QLatin1String("_"), QLatin1String("/usr/share/ukui-sidebar/ukui-quick-operation-panel")))
        QApplication::installTranslator(translator);
    else
        qDebug() << "cannot load translator ukui-quick-operation-panel_" << QLocale::system().name() << ".qm!";
}


void quickOperationWidget::updateTabWidget()
{
    QPalette pal = m_pMainTabWidget->palette();
    QColor color = qApp->palette().base().color();
    color.setAlphaF(0);
    pal.setColor(QPalette::Base, color);
    m_pMainTabWidget->setPalette(pal);

    QPalette tabPal = m_pMainTabWidget->tabBar()->palette();
    tabPal.setColor(QPalette::Base, color);

    QColor inactiveColor = qApp->palette().color(QPalette::BrightText);
    inactiveColor.setAlphaF(0.1);
    tabPal.setColor(QPalette::Window, inactiveColor);

    m_pMainTabWidget->tabBar()->setPalette(tabPal);
}

mainTabWidget::mainTabWidget()
{

}

void mainTabWidget::paintEvent(QPaintEvent *event)
{
//    QPainter p(this);
//    p.setRenderHint(QPainter::Antialiasing);
//    QPainterPath rectPath;
//    rectPath.addRoundedRect(this->rect().adjusted(0, 0, 0, 0), 12, 12);

//    QPixmap pixmap(this->rect().size());
//    pixmap.fill(Qt::transparent);
//    QPainter pixmapPainter(&pixmap);
//    pixmapPainter.setRenderHint(QPainter::Antialiasing);
//    pixmapPainter.setPen(Qt::transparent);
//    pixmapPainter.setBrush(Qt::black);
//    pixmapPainter.drawPath(rectPath);
//    pixmapPainter.end();

//    QImage img = pixmap.toImage();
//    qt_blurImage(img, 0, false, false);

//    pixmap = QPixmap::fromImage(img);
//    QPainter pixmapPainter2(&pixmap);
//    pixmapPainter2.setRenderHint(QPainter::Antialiasing);
//    pixmapPainter2.setCompositionMode(QPainter::CompositionMode_Clear);
//    pixmapPainter2.setPen(Qt::transparent);
//    pixmapPainter2.setBrush(Qt::transparent);
//    pixmapPainter2.drawPath(rectPath);
//    p.setOpacity(0);
//    p.drawPixmap(this->rect(), pixmap, pixmap.rect());
//    p.save();
//    p.fillPath(rectPath, qApp->palette().color(QPalette::Base));
//    p.restore();
//    QWidget::paintEvent(event);
}

MainWidget::MainWidget()
{
    getTransParency();
}

void MainWidget::getTransParency()
{
    m_dtranSparency = ControlCenterPersonGsetting::getInstance()->getTransparency();
    connect(ControlCenterPersonGsetting::getInstance(),&ControlCenterPersonGsetting::Sig_personChanged,
            this,[=](QString key) {
        if (key == CONTROL_CENTER_PERSONALISE_TRANS_KEY) {
            m_dtranSparency = ControlCenterPersonGsetting::getInstance()->getTransparency();
            setTransParency();
        }
    });
}

/* 设置当前匹配的透明度值
  设计要求，快速操作面板比通知中心透明度高0.3；
  思路如下:
  由于窗口透明度叠加，所以只能通过算比例;
  相当于100束光照进来，透明度为0.3，则剩下的光就是70束；
  在透过第二层为0.6的玻璃时，光束就只剩下28束，但是我们想要的是没有通过第一层过滤的，直接得到40束，表达式如下：
  100*(1 - 通知中心透明度) * X(所求的比例) = 100 * (1 - (通知中心透明度 + 0.3));
  当比例大于0.7小于0.9时，俩个透明度的比例差为0.1；
  当大于0.9.就直接将透明度设置为1，俩层叠加，视觉效果较明显
 */
void MainWidget::setTransParency()
{
    if (m_dtranSparency >= 0.7 && m_dtranSparency < 0.9) {
        m_dtranSparency = (1 - (m_dtranSparency + 0.1)) / (1.0 - m_dtranSparency);
    } else if (m_dtranSparency < 0.7) {
        m_dtranSparency = (1 - (m_dtranSparency + 0.3)) / (1.0 - m_dtranSparency);
    } else {
        m_dtranSparency = 1;
    }
    return;
}

void MainWidget::paintEvent(QPaintEvent *event)
{
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing);
    QPainterPath rectPath;
    rectPath.addRoundedRect(this->rect().adjusted(8, 8, -8, -8), 12, 12);

    QPixmap pixmap(this->rect().size());
    pixmap.fill(Qt::transparent);
    QPainter pixmapPainter(&pixmap);
    pixmapPainter.setRenderHint(QPainter::Antialiasing);
    pixmapPainter.setPen(Qt::transparent);
    pixmapPainter.setBrush(Qt::black);
    pixmapPainter.drawPath(rectPath);
    pixmapPainter.end();

    QImage img = pixmap.toImage();
    qt_blurImage(img, 8, false, false);

    pixmap = QPixmap::fromImage(img);
    QPainter pixmapPainter2(&pixmap);
    pixmapPainter2.setRenderHint(QPainter::Antialiasing);
    pixmapPainter2.setCompositionMode(QPainter::CompositionMode_Clear);
    pixmapPainter2.setPen(Qt::transparent);
    pixmapPainter2.setBrush(Qt::transparent);
    pixmapPainter2.drawPath(rectPath);
    p.setOpacity(0.7);
    p.drawPixmap(this->rect(), pixmap, pixmap.rect());
    p.save();
    p.fillPath(rectPath, qApp->palette().color(QPalette::Base));
    p.restore();
    QWidget::paintEvent(event);
}
